// pages/addplan/addplan.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: ['城区管网巡查', '截污干管巡查', '市政设施巡查'],
    typeIndex: 0,
    person: ['1号审核人', '2号审核人', '3号审核人'],
    personIndex: 0,
    items: [{
        value: '西湖区龙井路'
      },
      {
        value: '杨公堤'
      },
      {
        value: '西湖区南山路'
      },
      {
        value: '西湖区桃源岭'
      },
      {
        value: '上城区钱王祠路'
      },
      {
        value: '上城区西湖大道'
      },
      {
        value: '上城区解放路'
      },
      {
        value: '江南大道'
      },
      {
        value: '滨江区高新技术开发区(滨江)六和路'
      },
      {
        value: '中河北路'
      },
      {
        value: '建国北路'
      },
    ],
    range: '选择道路',
    rangeChoose: false,
    state: "",
    data: "",
    focus: '',
    textareaShow: true,
    content: '',
  },
  bindinput: function(e) {
    this.data[e.currentTarget.dataset.val] = e.detail.value;
  },
  fnTextereaTap: function() {
    this.setData({
      focus: true,
    })
  },
  fnTextareaInput: function(e) {
    this.setData({
      content: e.detail.value,
    })
  },
  fnChooseTap: function() {
    let data = this.data.data;
    data.road = "";
    this.setData({
      data,
      rangeChoose: !this.data.rangeChoose,
      textareaShow: !this.data.textareaShow,
    })
  },
  fnCloseTap: function() {
    this.setData({
      rangeChoose: false,
      textareaShow: true,
    })
  },
  fnCheckboxtap: function(e) {
    // console.log(e.target.dataset.index);
    let list = this.data.items;
    list[e.target.dataset.index].checked = !list[e.target.dataset.index].checked;
    this.setData({
      items: list
    })
  },
  checkboxChange: function(e) {
    let range = '';
    if (e.detail.value.length) {
      for (var i = 0; i < e.detail.value.length; i++) {
        range += e.detail.value[i] + '、'
      }
      range = range.slice(0, range.length - 1)
    } else {
      range = '选择道路';
    };
    this.setData({
      range,
    })
  },
  bindTypeChange: function(e) {
    this.setData({
      typeIndex: e.detail.value
    })
  },
  bindPersonChange: function(e) {
    this.setData({
      personIndex: e.detail.value
    })
  },
  // 点击保存、提交
  fnBtnTap: function(e) {
    var that = this;
    let data = this.data.data;
    let planss=this.data.plans;
    let plans = this.data.plans[this.current];
    console.log(plans)
    let index;
    for (var i = 0; i < plans.length; i++) {
      if (data.date == plans[i].date) {
        index = i;
      }
    }
    if (e.target.dataset.data == '提交') {
      plans[index].type = that.data.type[that.data.typeIndex];
      plans[index].road = that.data.range;
      plans[index].content = that.data.content;
      plans[index].state = '审批中';
    };
    if (e.target.dataset.data == '删除') {
      plans[index].type = "";
      plans[index].road = "";
      plans[index].content = "";
      plans[index].state = "";
    };
    if (e.target.dataset.data == '撤回') {
      plans[index].state = '未提交';
    };
    if (e.target.dataset.data == '保存') {
      plans[index].type = that.data.type[that.data.typeIndex];
      plans[index].road = that.data.range;
      plans[index].content = that.data.content;
      plans[index].state = "未提交";
    };
    //由子页面传值给父页面
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    prevPage.setData({
      plans:planss
    });

    this.setData({
      cover: true,
      coverText: e.target.dataset.data,
      textareaShow: false,
      focus: false,
    })
    setTimeout(function() {
      that.setData({
        cover: false,
        coverText: '',
        textareaShow: true,
      });
      wx.navigateBack({
        delta: 1
      });
    }, 2000)
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let data = JSON.parse(options.data);
    let plans = JSON.parse(options.plans);
    let current=options.current;
    this.current=current;
    let typeIndex = this.data.type.indexOf(data.type);
    if (typeIndex == '-1') {
      typeIndex = 0
    }
   
    if(!data.state){
      data.state='';
    }
    this.setData({
      typeIndex,
      data,
      content: data.content||'',
      plans
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})